package com.xingdata.jjxc.activity;

import jar.MessageManager;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.alibaba.fastjson.JSONObject;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * FM设置
 * 
 * @author Administrator
 * 
 */
public class FMSetting extends Base1Activity implements OnCheckedChangeListener {
	private EditText fm_name;
	private RadioGroup fM_SetttingGroup;
	private LinearLayout FM_Name_LinearLayout;
	private static final int DECIMAL_DIGITS = 2;

	@Override
	protected int getContentViewId() {
		return R.layout.fm_setting;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "FM设置";
	}

	@Override
	protected void initViews() {
		fm_name = (EditText) findViewById(R.id.FM_Name);
		fM_SetttingGroup = ((RadioGroup) findViewById(R.id.FM_Setting_RadioGroup));
		FM_Name_LinearLayout = (LinearLayout) findViewById(R.id.FM_Name_LinearLayout);
		fM_SetttingGroup.setOnCheckedChangeListener(this);
		// 过滤小数点
		InputFilter lengthfilter = new InputFilter() {

			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {
				if ("".equals(source.toString())) {
					return null;
				}
				String dValue = dest.toString();
				if (source.equals(".")
						&& (dValue.indexOf(".") > -1 || dValue.equals(""))) {
					return "";
				}
				String[] splitArray = dValue.split("\\.");
				if (splitArray.length > 1) {
					String dotValue = splitArray[1];
					int diff = dotValue.length() + 1 - DECIMAL_DIGITS;
					if (diff > 0) {
						return source.subSequence(start, end - diff);
					}
				}
				return null;
			}
		};

		fm_name.setFilters(new InputFilter[] { lengthfilter });
		initRadiGroup();
	}

	private void initRadiGroup() {
		((RadioButton) fM_SetttingGroup.findViewWithTag(SharedPreTools
				.getString(this, fM_SetttingGroup.getTag().toString(), "9")))
				.setChecked(true);
	}

	public void onSure(View view) {
		String strFmName = fm_name.getText().toString().trim();
		if (((RadioButton) fM_SetttingGroup.findViewWithTag("0")).isChecked()) {
			
			if (strFmName.length() == 0) {
				showToast("请输入频道88-108MHz");
				return;
			}
			Float f = Float.parseFloat(strFmName);
			if (f < 88 || f > 108) {
				showToast("请输入频道88-108MHz");
				return;
			}
		}else {
			strFmName="";
		}
		RequestEntity entity = new RequestEntity();
		String fmFlag = fM_SetttingGroup
				.findViewById((fM_SetttingGroup.getCheckedRadioButtonId()))
				.getTag().toString();
		entity.setType(InstructionSet.INSTRUCTION_AUTH_FM_OPEN + "");
		FMSettingEntity settingEntity = new FMSettingEntity();
		settingEntity.setFlag(fmFlag);
		if (strFmName.length()!=0) {
			settingEntity.setFm_name((int)Float.parseFloat(strFmName)*100+"");
		}
		entity.setMsg(JSONObject.toJSON(settingEntity).toString());
		MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), entity);
		finish();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
		for (int i = 0; i < group.getChildCount(); i++) {
			RadioButton mButton = (RadioButton) group.getChildAt(i);
			if (!mButton.isChecked()) {
				mButton.setTextColor(getResources()
						.getColor(R.color.txt_black1));
			} else {
				mButton.setTextColor(getResources().getColor(R.color.white));
			}
		}
		if (radioButton.getTag().equals("9")) {
			FM_Name_LinearLayout.setVisibility(View.GONE);
		} else {
			FM_Name_LinearLayout.setVisibility(View.VISIBLE);
		}
		SharedPreTools.putString(this, group.getTag().toString(), radioButton
				.getTag().toString());

	}

	class FMSettingEntity {
		String fm_name;
		String flag;

		public String getFm_name() {
			return fm_name;
		}

		public void setFm_name(String fm_name) {
			this.fm_name = fm_name;
		}

		public String getFlag() {
			return flag;
		}

		public void setFlag(String flag) {
			this.flag = flag;
		}

	}

}
